package cn.nebula.music.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.nebula.music.domain.User;
import cn.nebula.music.service.AlbumService;
import cn.nebula.music.service.CombinationService;
import cn.nebula.music.service.PictureService;
import cn.nebula.music.service.RoleService;
import cn.nebula.music.service.SingerService;
import cn.nebula.music.service.SonglistService;
import cn.nebula.music.service.UserService;

@Controller
public class UserController {

	@Autowired
	private UserService userService;

	@Autowired
	private AlbumService albumService;

	@Autowired
	private SingerService singerService;

	@Autowired
	private CombinationService combinationService;

	@Autowired
	private SonglistService songlistService;

	@Autowired
	private RoleService roleService;

	@Autowired
	private PictureService pictureService;

	@RequestMapping("/listAllUsers")
	@ResponseBody
	public Map<String, Object> listAllUsers() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("users", userService.findAllUsersOfBT());
		return values;
	}

	@RequestMapping("/editUser")
	@ResponseBody
	public Map<String, Object> editUesr(@RequestParam Integer id) throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		System.out.println(pictureService.findAllPicturesOfBT() + "111");
		values.put("user", userService.findUserById(id));
		values.put("roles", roleService.findAllRolesOfBT());
		values.put("pictures", pictureService.findAllPicturesOfBT());
		values.put("albums", albumService.findAllAlbumsOfBT());
		values.put("combinations", combinationService.findAllCombinationsOfBT());
		values.put("singers", singerService.findAllSingersOfBT());
		values.put("songlists", songlistService.findAllSonglistsOfBT());
		return values;
	}

	@RequestMapping("/newUser")
	@ResponseBody
	public Map<String, Object> newUser() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("albums", albumService.findAllAlbumsOfBT());
		values.put("pictures", pictureService.findAllPicturesOfBT());
		values.put("combinations", combinationService.findAllCombinationsOfBT());
		values.put("singers", singerService.findAllSingersOfBT());
		values.put("songlists", songlistService.findAllSonglistsOfBT());
		values.put("roles", roleService.findAllRolesOfBT());
		return values;
	}

	@RequestMapping("/updateUser")
	@ResponseBody
	public boolean updateUser(@RequestBody User user) throws Exception {
		userService.updateUser(user);
		return true;
	}

	@RequestMapping("/addUser")
	@ResponseBody
	public boolean addeUser(@RequestBody User user) throws Exception {
		userService.addUser(user);
		return true;
	}

	@RequestMapping("/deleteUser")
	@ResponseBody
	public boolean deleteUser(@RequestParam Integer id) throws Exception {
		userService.deleteUserById(id);
		return true;
	}

	@RequestMapping("/toLogin")
	public String toLogin() {
		return "redirect:/login";
	}

	@RequestMapping("/toRegister")
	public String toRegister() {
		return "redirect:/register";
	}

	@RequestMapping("/login")
	@ResponseBody
	public Boolean login(@RequestBody User user, HttpSession session) throws Exception {
		String inf = user.getUsername();
		User loginUser = new User();
		if (inf.contains("@")) {
			loginUser.setEmail(inf);
		} else if (Pattern.compile("[0-9]*").matcher(inf).matches() && inf.length() == 11) {
			loginUser.setPhone(inf);
		} else {
			loginUser.setUsername(inf);
		}
		loginUser.setPassword(user.getPassword());
		Boolean result = userService.loginValidate(loginUser);
		if (result) {
			if (loginUser.getUsername() != null)
				session.setAttribute("USER", userService.findUserByUsername(loginUser.getUsername()));
			else if (loginUser.getEmail() != null)
				session.setAttribute("USER", userService.findUserByEmail(loginUser.getEmail()));
			else if (loginUser.getPhone() != null)
				session.setAttribute("USER", userService.findUserByPhone(loginUser.getPhone()));

			System.out.println(session.getAttribute("USER"));
		}
		return result;
	}

	@RequestMapping("/logout")
	@ResponseBody
	public void logout(HttpSession session) throws Exception {
		session.removeAttribute("USER");
	}

	@RequestMapping("/register")
	@ResponseBody
	public String register(@RequestBody User user) throws Exception {
		String inf = user.getUsername();
		User loginUser = new User();
		if (inf.contains("@")) {
			loginUser.setEmail(inf);
		} else if (Pattern.compile("[0-9]*").matcher(inf).matches() && inf.length() == 11) {
			loginUser.setPhone(inf);
		} else {
			loginUser.setUsername(inf);
		}

		loginUser.setPassword(user.getPassword());
		String result = userService.registerValidate(loginUser);
		return result;
	}
}